package com.example.javabasic.thread.classtwo.singleton.demo4;

import com.example.javabasic.thread.classtwo.singleton.demo3.RunnableState;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class ThreadLifeStyleListener implements LifeStyleListener {


    //执行逻辑
    public void concurrentQuery(Runnable runnable) {
        if (runnable == null) {
            return;
        }
        //这个线程也可以是从线程池中获取
        new Thread(new ObserverRunnable(this) {
            @Override
            public void run() {
                try {
                    //状态改为运行中
                    notifyChange(new RunnableEvent(RunnableState.RUNNING, Thread.currentThread(), null));
                    runnable.run();
                    //修改状态为完成
                    notifyChange(new RunnableEvent(RunnableState.DONE, Thread.currentThread(), null));
                } catch (Throwable e) {
                    //状态改为错误
                    notifyChange(new RunnableEvent(RunnableState.ERROR, Thread.currentThread(), e));
                }
            }
        }).start();
//        //创建一个线程, 并创建 runnable任务
//        new Thread(new ObserverRunnable(this) {
//            @Override
//            public void run() {
//                try {
//                    //状态改为运行中
//                    notifyChange(new RunnableEvent(RunnableState.RUNNING,Thread.currentThread(),null));
//                    //线程运行5s
//                    Thread.sleep(2000);
//                    //修改状态为完成
//                    notifyChange(new RunnableEvent(RunnableState.DONE,Thread.currentThread(),null));
//                }catch (Throwable e){
//                    //状态改为错误
//                    notifyChange(new RunnableEvent(RunnableState.ERROR,Thread.currentThread(),e));
//                }
//            }
//        },id).start();

    }

}
